package cn.byapps.attendance.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.byapps.attendance.entity.Student;
import cn.byapps.attendance.entity.Teacher;

public class LoginUserInterceptor implements HandlerInterceptor {

	public LoginUserInterceptor() {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Student u = (Student) request.getSession().getAttribute("loginUser");
		Teacher t = (Teacher) request.getSession().getAttribute("loginAdmin");
		if (u != null || t != null) {
			return true;
		} else {
			String uri = request.getContextPath();
			response.sendRedirect(uri + "/login");
			return false;
		}
	}

}
